package com.weng.platform.apiManager.doc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vladsch.flexmark.ast.Node;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import com.weng.framework.dao.mybatis.service.BaseServiceImpl;
import com.weng.platform.apiManager.doc.entity.ApiDoc;
import com.weng.platform.apiManager.doc.mapper.ApiDocMapper;
import com.weng.platform.apiManager.doc.service.IApiDocService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;

/**
 * api文档 服务实现类
 *
 * @author wengzhonghui
 * @date 2020-09-24 23:45:15
 *
 */ 
@Slf4j
@Service
public class ApiDocServiceImpl extends BaseServiceImpl<ApiDocMapper, ApiDoc> implements IApiDocService {
	@Autowired
	private ApiDocMapper apiDocMapper;
	

	public int countByCatalogId(String catalogId){
	    QueryWrapper<ApiDoc> qw = new QueryWrapper<>();
	    qw.eq("catalog_id",catalogId);
	    return apiDocMapper.selectCount(qw);
    }
	/**
	 * 分页查询api文档
	 * @return
	 */
	@Override
	public IPage<ApiDoc> list(Integer pageNo,Integer pageSize, String projectId,String title, String catalogId) {
		LambdaQueryWrapper<ApiDoc> qw = new QueryWrapper<ApiDoc>().lambda();
		qw.select(ApiDoc::getId,ApiDoc::getTitle,ApiDoc::getPosition,ApiDoc::getAuthorId,ApiDoc::getAuthorUsername,ApiDoc::getProjectId,ApiDoc::getUpdateTime);
		projectId = StringUtils.isEmpty(projectId) ? "-1" : projectId;
		qw.eq(ApiDoc::getProjectId, projectId);
		if(StringUtils.isNotEmpty(catalogId)){
			qw.eq(ApiDoc::getCatalogId, catalogId);
			qw.orderByAsc(ApiDoc::getPosition);
		}
		if(StringUtils.isNotEmpty(title)){
				qw.like(ApiDoc::getTitle,  title  + "%" );
		}
		IPage<ApiDoc> page = new PageModel<>(pageNo,pageSize);
		return apiDocMapper.selectPage(page,qw);
	}


	/**
	 * 获取单个api文档
	 * @param id
	 * @return
	 */
	@Override
	public ApiDoc getById(Serializable id) {
		return super.getById(id);
	}

	/**
	 * 新增单个api文档
	 * @param apiDoc
	 * @return
	 */
	@Override
	public ApiDoc add(ApiDoc apiDoc)  {
	    if(StringUtils.isNotEmpty(apiDoc.getContent())){
            apiDoc.setHtmlContent(markdownToHtml(apiDoc.getContent()));
        }
		apiDocMapper.insert(apiDoc);
		return apiDoc;
	}

	/**
	 * 更新单个api文档
	 * @param apiDoc
	 * @return
	 * @
	 */
	@Override
	public int update(ApiDoc apiDoc)  {
        if(StringUtils.isNotEmpty(apiDoc.getContent())){
            apiDoc.setHtmlContent(markdownToHtml(apiDoc.getContent()));
        }
		return apiDocMapper.updateById(apiDoc);

	}


	@Override
	public ResponseData removeByIds(String ids) {
		String[] apiDocIds = ids.split(",");
		this.removeByIds(Arrays.asList(apiDocIds));
		return ResponseData.success();
	}
    
	/*
	 * markdown文档转html内容
	 * @param [content]
	 * @return
	 */
	private static  String markdownToHtml(String content){
        Parser parser = Parser.builder().build();
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        Node document = parser.parse(content);
        return renderer.render(document);
    }


}
